//
// Copied. on 2022/4/17.
//

#ifndef UNTITLED_INDEX_H
#define UNTITLED_INDEX_H

#ifndef __INDEX_H__
#define __INDEX_H__
#include "stx/btree_set.h"
// set rid to -1 when using tempBuffer in the table to compare.
struct IndexKey {
    int64_t keyValue;
    int pageID;
    int pageRowOff;
    IndexKey() = default;
    IndexKey( int64_t _value, int _pageID, int _pageRowOff) {
        keyValue = _value;
        pageID = _pageID;
        pageRowOff = _pageRowOff;
    }
    int64_t getValue() const {return keyValue;}
    int getPageId() const { return pageID; }
    int getPageRowOff() const { return pageRowOff; }
    friend bool operator<(const IndexKey &a, const IndexKey &b){
        return a.keyValue < b.keyValue ? true : false;
    };
};

struct Index {
    stx::btree_set<IndexKey> list;
    stx::btree_set<IndexKey>::iterator iter;
    std::string genFilename(char* tableName, int col);
    void clear();
    bool load(char* tableName, int col);
    void store(char* tableName, int col);
    void drop(char* tableName, int col);
    void erase(const IndexKey &key);
    bool insert(const IndexKey &key);
    int begin();
    int end();
    int lowerBound(const IndexKey &key);
    int upperBound(const IndexKey &key);
    int lowerBoundEqual(const IndexKey &key);
    int next();
    int nextEqual(const IndexKey &key);
    int reversedNext();
};

#endif



#endif //UNTITLED_INDEX_H
